﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ClassLibrary;
using Param = ClassLibrary.Parameters.Values;
using System.IO;

namespace ControlLibrary
{
    public partial class Form_Publish : Form
    {
        List<string> _Upload_small;
        List<string> _Upload_med;
        List<string> _Upload_files;
        bool _tryAgain;
        int nbFiles;

        public bool TryAgain
        {
            get { return _tryAgain; }
        }

        public delegate void OnTryAgain();
        [Category("Configuration"), Browsable(true), Description("")]
        public event OnTryAgain onTryAgain;

        /*public Form_Publish(List<string> Upload_small, List<string> Upload_med, List<string> Upload_files)
        {
            InitializeComponent();

            _Upload_small = Upload_small;
            _Upload_med = Upload_med;
            _Upload_files = Upload_files;

            nbFiles = Upload_small.Count + Upload_med.Count + Upload_files.Count;

            label_Msg.Visible = false;
            label_publicationInProgress.Visible = true;
            linkLabel_Parameters.Visible = false;
            label_file.Visible = true;

            backgroundWorker1.RunWorkerAsync();
        }*/

        public Form_Publish(List<Marker> markerList)
        {
            InitializeComponent();

            _tryAgain = false;
            //small & medium img for each marker and dataset.js plus MyGoogleMap.html
            int n = (from marker in markerList
                     where marker.published == false
                     select marker).Count();

            nbFiles = n * 2 + 2;
            label_FileNum.Text = "(0 /" + nbFiles + ")";
            label_file.Text = "";

            panel_msg.Visible = true;
            panel_Error.Visible = false;


        }

        private void Form_Publish_Shown(object sender, EventArgs e)
        {
            backgroundWorker1.RunWorkerAsync();
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            bool RC = true;
            int n = 0;
            try
            {
                foreach (Marker m in Param.MarkerList)
                {
                    if (!m.published)
                    {
                        string file_Small = Path.Combine(Param.localPath_Small, Path.GetFileName(m.Img_Small));
                        string file_Med = Path.Combine(Param.localPath_Medium, Path.GetFileName(m.Img_Medium));

                        //Small
                        this.Invoke(new MethodInvoker(delegate()
                        {
                            n++;
                            label_FileNum.Text = "(" + n + "/" + nbFiles + ")";
                            label_file.Text = PathUtility.getLeaf(file_Small);
                        }));
                        RC = FTPUtility.Upload(file_Small, Param.ServerPath_Small, Path.GetFileName(file_Small));

                        if (true == RC)
                        {
                            //Medium
                            this.Invoke(new MethodInvoker(delegate()
                            {
                                n++;
                                label_FileNum.Text = "(" + n + "/" + nbFiles + ")";
                                label_file.Text = PathUtility.getLeaf(file_Med);
                            }));
                            RC = FTPUtility.Upload(file_Med, Param.ServerPath_Medium, Path.GetFileName(file_Med));
                        }

                        if (!RC)
                            break;
                        else m.published = true;
                    }
                }

                //File
                string[] files = { Param.localPath_DataSet, Param.localPath_GoogleMap };
                foreach (string file in files)
                {
                    this.Invoke(new MethodInvoker(delegate()
                    {
                        n++;
                        label_FileNum.Text = "(" + n + "/" + nbFiles + ")";
                        label_file.Text = PathUtility.getLeaf(file);
                    }));


                    RC = FTPUtility.Upload(file, Param.ServerPath_ProjectFolder, Path.GetFileName(file));

                    if (!RC)
                        break;
                }

                if (!RC)
                {
                    this.Invoke(new MethodInvoker(delegate()
                        {
                            panel_msg.Visible = false;
                            panel_Error.Visible = true;

                            progressBar1.Visible = false;
                        }));
                }
                else
                {
                    this.Invoke(new MethodInvoker(delegate()
                    {
                        this.Close();
                    }));
                }
            }
            catch (Exception ex)
            {
                ErrorManager.WriteLog("Form_Publish", "backgroundWorker1_DoWork", ex);
            }
        }

        private void linkLabel_Parameters_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Form_Parameters param_form = new Form_Parameters(0);
            param_form.ShowDialog(this);
        }

        private void linkLabel_TryAgain_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            _tryAgain = true;
            this.Close();
        }

    }
}
